package com.potenza.activiti.controller;

import com.potenza.activiti.security.SecurityUtil;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

@RestController
public class LoanRequestActivitiController {

    @Autowired
    private ProcessRuntime processRuntime;//
    @Autowired
    private TaskRuntime taskRuntime;//

    @Autowired
    private SecurityUtil securityUtil;
    //
    public Map submitLoanRequest(String openid, String cfoId, String check_id,
                                    Long loanRequestId,
                                    BigDecimal unUsedCredit,
                                    BigDecimal requestAmount){

        //参与者 openid，cfoId，check_id
        // businessKey 定义了流程类型 type:loanRequest, id:贷款申请id
        //剩余信用
            return null;
    }

    /**
     * CFO 审核
     * @param taskId
     * @param cfoId
     * @param success
     * @return
     */
    @GetMapping("/CFOAudit/{taskId}/{cfoId}/{success}")
    public Map CFOAudit(@PathVariable String taskId,
                        @PathVariable String cfoId,
                        @PathVariable boolean success) {

        //
        Task task = taskRuntime.task(taskId);

        if(task!=null){
            taskRuntime.complete(
                    TaskPayloadBuilder
                            .complete()
                            .withTaskId(task.getId())
                            .withVariable("cfo_audit",success)
                            .build());
            String processInstanceId = task.getProcessInstanceId();
            ProcessInstance processInstance = processRuntime.processInstance(processInstanceId);
            System.out.println("processInstance Status"+processInstance.getStatus());
        }
        return null;
    }

    @GetMapping("/checker/{taskId}/{checkId}/{success}")
    public Map checker(@PathVariable String taskId,
                        @PathVariable String checkId) {

        //
        Task task = taskRuntime.task(taskId);
        if(task!=null){
            taskRuntime.complete(
                    TaskPayloadBuilder
                            .complete()
                            .withTaskId(task.getId())
                            .build());
            String processInstanceId = task.getProcessInstanceId();
            ProcessInstance processInstance = processRuntime.processInstance(processInstanceId);
            System.out.println("processInstance Status"+processInstance.getStatus());
        }
        return null;
    }


}
